package cn.edu.jju.psytest.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.jju.psytest.model.entity.QuizRecord;
import cn.edu.jju.psytest.model.vo.TestResultVo;
import cn.edu.jju.psytest.persist.QuizRecordDao;
import cn.edu.jju.psytest.persist.util.QuizRecordUtil;

@SuppressWarnings("restriction")
@Service
@Transactional(readOnly = true)
public class QuizRecordService {

	private QuizRecordDao quizRecordDao;

	@Transactional(readOnly = false)
	public void saveQuizRecord(QuizRecord quizRecord) {
		quizRecordDao.saveQuizRecord(quizRecord);
	}

	public String getQuizTestTime() {
		String testTimeS = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Iterator<?> iterator = quizRecordDao.getQuizTestTime().iterator();
		while (iterator.hasNext()) {
			Date date = (Date) iterator.next();
			testTimeS += sdf.format(date) + ",";
		}
		if(testTimeS.equals("")) {
			return "no_record";
		}
		testTimeS = testTimeS.substring(0, testTimeS.lastIndexOf(","));
		return testTimeS;
	}
	
	public TestResultVo getQuizTestResult(String userId) {
		List<?> list = quizRecordDao.getQuizAnswers(userId);
		return QuizRecordUtil.getTestResult((String)list.get(0));
	}

	/**
	 * Getters And Setters
	 */
	public QuizRecordDao getQuizRecordDao() {
		return quizRecordDao;
	}

	@Resource
	public void setQuizRecordDao(QuizRecordDao quizRecordDao) {
		this.quizRecordDao = quizRecordDao;
	}
}
